﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Toon.Dialogs;
using Toon.Models.Catalogs;
using Toon.Models.Characters;
using AttributeAlias = Toon.Models.Characters.Attribute;

namespace Toon.Controls
{
	public partial class CharacterSheet : UserControl
	{
		public CharacterSheet()
		{
			this.AddConverter<PossessionType, Visibility>("UnusualItem", v => v == PossessionType.Normal ? Visibility.Collapsed : Visibility.Visible);

			// Required to initialize variables
			InitializeComponent();
		}

		public Character CurrentCharacter
		{
			get { return ((PrimaryViewModel)DataContext).CurrentCharacter; }
		}

		private void Name_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = SimpleTextWindow.Show(this, "Name", "What is your character called?", new Binding("Name") { Source = CurrentCharacter, Mode = BindingMode.TwoWay });
		}

		private void Occupation_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = SimpleTextWindow.Show(this, "Occupation", "What does your character do?", new Binding("Occupation") { Source = CurrentCharacter, Mode = BindingMode.TwoWay });
		}

		private void BeliefsAndGoals_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = SimpleTextWindow.Show(this, "Beliefs & Goals", "What does your character believe in?", new Binding("BeliefsAndGoals") { Source = CurrentCharacter, Mode = BindingMode.TwoWay }, textBoxLines: 3);
		}

		private void NaturalEnemies_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = SimpleTextWindow.Show(this, "Natural Enemies", "Does your character have any natural enemies?", new Binding("NaturalEnemies") { Source = CurrentCharacter, Mode = BindingMode.TwoWay }, textBoxLines: 3);
		}

		private void Species_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = SimpleTextWindow.Show(this, "Species", "What is your character?", new Binding("Species") { Source = CurrentCharacter, Mode = BindingMode.TwoWay });
		}

		private void Attribute_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = SkillPicker.Show(this, (AttributeAlias)((Control)sender).DataContext);
		}

		private void Shitcks_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = ShitckPicker.Show(this, CurrentCharacter);
		}

		private void Possessions_Click(object sender, RoutedEventArgs e)
		{
			m_ChildWindow = PossessionPicker.Show(this, CurrentCharacter);
		}

		private void Description_Click(object sender, RoutedEventArgs e)
		{
			SimpleTextWindow.Show(this, "Description", "Describe your character in his own words", new Binding("Description") { Source = CurrentCharacter, Mode = BindingMode.TwoWay }, textBoxLines: 3);
		}

		public bool HasOpenDialogs
		{
			get
			{
				return m_ChildWindow != null && m_ChildWindow.IsOpen;
			}

		}

		private ImprovedChildWindow m_ChildWindow;

		internal void CloseDialogs()
		{
			m_ChildWindow.Close();
		}


		public static readonly DependencyProperty EditModeProperty = DependencyProperty.Register("EditMode", typeof(EditMode), typeof(CharacterSheet), new PropertyMetadata(EditMode.SkillPoints, OnEditModeChanged));


		private static void OnEditModeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			CharacterSheet characterSheet = o as CharacterSheet;
			if (characterSheet != null)
				characterSheet.OnEditModeChanged((EditMode)e.OldValue, (EditMode)e.NewValue);
		}

		protected virtual void OnEditModeChanged(EditMode oldValue, EditMode newValue)
		{
			if (newValue == Controls.EditMode.Printable)
			{
				EditableAttributesA.Visibility = Visibility.Collapsed;
				EditableAttributesB.Visibility = Visibility.Collapsed;
				PrintableAttributes.Visibility = Visibility.Visible;
			}
			else
			{
				EditableAttributesA.Visibility = Visibility.Visible;
				EditableAttributesB.Visibility = Visibility.Visible;
				PrintableAttributes.Visibility = Visibility.Collapsed;
			}
		}

		public EditMode EditMode
		{
			get { return (EditMode)GetValue(EditModeProperty); }
			set { SetValue(EditModeProperty, value); }
		}


       
	}
}